package com.wc.codeforces.贪心.K_Sort;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/6/30 23:23
 * @description https://codeforces.com/contest/1987/problem/B
 */
public class Main {
    /**
     * 思路：
     * 要非递减
     * 最小的数一定是不变
     * 后面的数花最小的代价就是等于前面一个数，以此类推
     * 那可以选择k个数, k不限
     * 那我们每次选择最小的一个数, 然后贯穿始末, 次数越少, +1就越少
     * 理论成立, 开始实践
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1000010;
    static int[] a = new int[N];
    static int[] b = new int[N];
    static int n;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            solve();
        }
        out.flush();
    }

    static void solve() {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
        int len = 0;
        for (int i = 2; i <= n; i++) {
            if (a[i] < a[i - 1]) {
                b[++len] = a[i - 1] - a[i];
                a[i] = a[i - 1];
            }
        }
        Arrays.sort(b, 1, len + 1);
        long res = 0;
        int pre = 0;
        for (int i = 1; i <= len; i++) {
            if (b[i] > pre) {
                res += (long) (b[i] - pre) * (len - i + 1 + 1);
                pre = b[i];
            }
        }
        out.println(res);
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
